﻿using System.AddIn.Pipeline;
using System.IO;
using System.Windows;
using DCT.AddIn.Contract;
using DCT.AddIn.HostView;

namespace DCT.AddIn.HostSideAdapter
{
    [HostAdapter]
    public class DCTExtendedFunctionContractToViewHostAdapter : DCTExtendedFunctionHostView
    {
        private IDCTExtendedFunctionContract _contract;
        private ContractHandle contractHandle;

        public DCTExtendedFunctionContractToViewHostAdapter(IDCTExtendedFunctionContract contract)
        {
            _contract = contract;
            contractHandle = new ContractHandle(contract);
        }

        public override int VisualPosition
        {
            get { return _contract.VisualPosition; }
        }

        public override FrameworkElement GetVisual()
        {
            return FrameworkElementAdapters.ContractToViewAdapter(_contract.GetVisual());
        }

        public override void UpdateData()
        {
            _contract.UpdateData();
        }

        public override void Initialize(HostObject hostObj)
        {
            _contract.Initialize(new HostObjectViewToContractHostAdapter(hostObj));
        }
    }

    public class HostObjectViewToContractHostAdapter : ContractBase, IHostObjectContract
    {
        private HostObject _view;

        public HostObjectViewToContractHostAdapter(HostObject view)
        {
            _view = view;
        }

        public Stream GetData()
        {
            return _view.GetData();
        }

        public void ReportProgress(double progressPercent)
        {
            _view.ReportProgress(progressPercent);
        }

        public void ApplyChanges(Stream xmlChanges)
        {
            _view.ApplyChanges(xmlChanges);
        }
    }
}
